#include "Player.h"


Player::Player (sf::Vector2f position, Artman *artman,AnimationSolver *animator)
{
	m_frame=0; m_flip=false; m_frameclock=0;
	p_animator=animator;
	m_state=CS_fall; m_ticker=0;
	m_position=position;
	this->setPosition(position.x,position.y);
	this->setTexture(*artman->getTexture("sanhien.png"));
	this->setTextureRect(p_animator->getRect(0,m_state,&m_frame,false));
	this->setOrigin(32,32);
	p_plat=NULL;
	m_djumpstate=DJS_enabled;


	m_penis=new Penis(this);
	p_spritesheets[0]=artman->getTexture("sanhiencock.png");
	p_spritesheets[1]=artman->getTexture("sanhiencockfull.png");
	m_penis->setTexture(*p_spritesheets[0]);
	m_penis->setOrigin(32,32);
	m_type=AT_player;
	m_collision=CE_collide;
	m_loaded=false;
}

Player::~Player()
{
	delete m_penis;
}

void Player::ResetDjump()
{
	if (m_djumpstate>DJS_disabled)
	{
		m_djumpstate=DJS_enabled;
	}
}

void Player::SetJump()
{
	if (m_djumpstate==DJS_enabled)
	{
		m_djumpstate=DJS_jump;
	}
}
void Player::ReleaseDjump()
{
	if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && m_djumpstate==DJS_jump)
	{
		if (m_djumpstate==DJS_jump)
		{
			m_djumpstate=DJS_ready;
		}
	}
}

void Player::ClimaxAttack()
{
	m_loaded=false;
	m_penis->setTexture(*p_spritesheets[0]);
	m_state=CS_specialattack;
	m_ticker=20;
	p_scrotum->setVisible(false);
}

void Player::Doublejump()
{
	if (m_djumpstate==DJS_ready)
	{
		m_state=CS_jump;
		m_djumpstate=DJS_used;
		m_velocity.y=-20;
		if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		{
			if (m_velocity.x<4)
			{
				m_velocity.x=4;
			}
		}
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		{
			if (m_velocity.x>-4)
			{
				m_velocity.x=-4;
			}
		}
	}
}



void Player::PlayerInput(float dt)
{
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
	{
		if (Supported())
		{
			m_velocity.x=0;
			if (p_solver->getTileType(sf::Vector2f(m_position.x-16,m_position.y))==TT_shadow 
				&& p_solver->getTileType(sf::Vector2f(m_position.x+16,m_position.y))==TT_shadow)
			{
				m_state=CS_hide;
				m_collision=CE_nocollide;
			}
		}
	}
	else
	{
		if (m_state==CS_hide)
		{
			m_state=CS_stand;
			m_collision=CE_collide;
		}
		//controls for dropping down off of platforms and initiating jumps
		if 	(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && Supported())
		{
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
			{
				if (Supported() && Floorcheck()==TT_plat)
				{
					m_state=CS_fall;
					ReleaseDjump();
					m_position.y+=4;
				}
			}
			else
			{
				m_state=CS_crouch;
				m_velocity.x=0;
			}
		}
		else
		{
			if (m_state==CS_crouch && Supported())
			{
				m_state=CS_stand;
			}
		}
		if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && Supported() && m_state!=CS_prejump)
		{
			m_state=CS_prejump;
			SetJump();
			//m_velocity.y=-14;
			//m_position.y-=8;
			m_ticker=2;
			if (p_plat!=NULL)
			{
				p_plat=NULL;
			}
		}
		//control for the djump, not quite working right yet
		if (!Supported() && m_djumpstate==DJS_ready && sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
		{
			Doublejump();
		}
		//controls for variable jumps
		if (m_ticker>0 && m_state==CS_prejump)
		{
			m_ticker-=dt;
			if (m_ticker<=0)
			{
				m_ticker=0;
				if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
				{
					m_velocity.y=-22;
					m_position.y-=16;
					m_state=CS_jump;
				}
				else
				{
					m_velocity.y=-16;
					m_position.y-=8;
					m_state=CS_jump;
				}
			}
		}
		ReleaseDjump();

		//move left and right
		if (Supported())
		{
			if 	(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && m_velocity.x>-7)
			{
				m_state=CS_move;
				m_flip=true;
				m_velocity.x-=4*dt;
				if (m_velocity.x<-7)
				{
					m_velocity.x=-7;
				}
			}
			if 	(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && m_velocity.x<7)
			{
				m_state=CS_move;
				m_flip=false;
				m_velocity.x+=4*dt;
				if (m_velocity.x>7)
				{
					m_velocity.x=7;
				}
			}

		}
		else
		{
			if 	(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && m_velocity.x>-7)
			{
				m_velocity.x-=1*dt;
				if (m_velocity.x>0)
				{
					m_velocity.x-=1*dt;
				}
				else
				{
					m_flip=true;
				}
				if (m_velocity.x<-7)
				{
					m_velocity.x=-7;
				}
			}
			if 	(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && m_velocity.x<7)
			{
				m_velocity.x+=1*dt;
				if (m_velocity.x<0)
				{
					m_velocity.x+=1*dt;
				}
				else
				{
					m_flip=false;
				}
				if (m_velocity.x>7)
				{
					m_velocity.x=7;
				}
			}
		}
		//cockvore!

		if (sf::Keyboard::isKeyPressed(sf::Keyboard::X))
		{
			if (m_state==CS_stand)
			{
				if (p_scrotum->getVisible())
				{
					if (m_loaded)
					{
						ClimaxAttack();
					}
					else
					{
						m_state=CS_strokeoff;
					}
				}
				else
				{
					Character *a=NULL;
					if (m_flip)
					{
						a=(Character *)p_solver->getActorCollision(this,sf::Rect<int>(m_position.x-64,m_position.y-32,64,64),AT_enemy);
						//check in front of us for a target
						if (a!=NULL)
						{
							//check if target is eligible
							if (a->Vore(m_flip))
							{
								a->AsetPosition(sf::Vector2f(m_position.x-32,m_position.y));
								m_state=CS_stealthcv;
								m_ticker=4;
								p_scrotum->Consume(a->getMiniview());
							}
						}
	
					}
					else
					{
						a=(Character *)p_solver->getActorCollision(this,sf::Rect<int>(m_position.x,m_position.y-32,64,64),AT_enemy);
						if (a!=NULL)
						{
							//check if target is eligible
							if (a->Vore(m_flip))
							{
								a->AsetPosition(sf::Vector2f(m_position.x+32,m_position.y));
								m_state=CS_stealthcv;
								m_ticker=8;
								p_scrotum->Consume(a->getMiniview());
							}
						}
					}
				}
			}
		}
		else
		{
			if (p_scrotum->getVisible() && !m_loaded)
			{
				m_loaded=p_scrotum->Digest(0.1F*dt);
			}
			if (m_state==CS_strokeoff)
			{
				m_state=CS_stand;
			}
		}
	}
}

void Player::Land()
{
	ResetDjump();
}

void Player::HandleCV(float dt)
{
	m_ticker-=dt;
	if (m_ticker<=0)
	{
		m_state=CS_stand;
		p_scrotum->setVisible(true);
		m_penis->setTexture(*p_spritesheets[1]);
	}
}
void Player::HandleClimax(float dt)
{
	m_ticker-=dt;
	if (m_ticker<=0)
	{
		m_state=CS_stand;
	}
}

void Player::HandleStrokeoff(float dt)
{
	m_loaded=p_scrotum->Digest(dt);
	if (m_loaded)
	{
		ClimaxAttack();
	}
}

void Player::Update(float dt)
{
	m_frameclock+=dt;

	if (m_frameclock>1)
	{
		this->setTextureRect(p_animator->getRect(0,m_state,&m_frame,m_flip));
		m_frameclock=0;
	}
	//if we're on a platform, we need to factor in platform motion to our calculations
	if (p_plat!=NULL)
	{
		m_velocity+=p_plat->getVelocity();
	}
	Gravity(dt);
	Friction(dt);
	if (NotBusy())
	{
		PlayerInput(dt);
	}
	else
	{
		HandleCV(dt);
	}
	if (m_state==CS_specialattack)
	{
		HandleClimax(dt);
	}
	if (m_state==CS_strokeoff)
	{
		HandleStrokeoff(dt);
	}
	Collision(dt);

	m_position+=(m_velocity*dt);
	this->setPosition(m_position);
	if (p_plat!=NULL)
	{
		m_velocity-=p_plat->getVelocity();
	}
	m_penis->Update(dt,p_animator);
}

void Player::ExtraDraw(sf::RenderWindow &window)
{
	window.draw(*m_penis);
}

void Player::LinkScrotum(Scrotum *sack)
{
	p_scrotum=sack;
}